package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ReturnedPurchaseProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseProductEntity;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.model.ReturnedPurchaseProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJian
 * @date 2019-01-25 15:38
 **/
@Service("ReturnedPurchaseProductModel")
public class ReturnedPurchaseProductModelImpl implements ReturnedPurchaseProductModel{
    @Resource
    private ReturnedPurchaseProductDao returnedPurchaseProductDao;
    @Resource
    private ProductModel productModel;
    /**
     * 插入数据
     *
     * @param returnedPurchaseProductEntity 采购退货单产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer insert(ReturnedPurchaseProductEntity returnedPurchaseProductEntity) {
        returnedPurchaseProductEntity.setAddTime(DateTimeUtil.getInt());
        returnedPurchaseProductEntity.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(returnedPurchaseProductEntity);
        return returnedPurchaseProductDao.insert(returnedPurchaseProductEntity);
    }

    @Override
    public Integer insertBatch(List<ReturnedPurchaseProductEntity> productAddList) {
        if (CollectionUtils.isEmpty(productAddList)) {
            return 0;
        }
        StandardizeDataUtil.standardize4ReturnedPurchaseProductEntityList(productAddList);
        return returnedPurchaseProductDao.insertBatch(productAddList);
    }

    /**
     * 更新数据
     *
     * @param returnedPurchaseProductEntity 采购退货单产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer update(ReturnedPurchaseProductEntity returnedPurchaseProductEntity) {
        returnedPurchaseProductEntity.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(returnedPurchaseProductEntity);
        return returnedPurchaseProductDao.update(returnedPurchaseProductEntity);
    }

    /**
     * 保存
     *
     * @param returnedPurchaseProductEntity 插入实体
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer save(ReturnedPurchaseProductEntity returnedPurchaseProductEntity) {
        if (returnedPurchaseProductEntity.getId() == null || returnedPurchaseProductEntity.getId().equals(0L)) {
            return returnedPurchaseProductDao.insert(returnedPurchaseProductEntity);
        }
        return returnedPurchaseProductDao.update(returnedPurchaseProductEntity);
    }

    /**
     * 根据主键删除记录
     *
     * @param key    主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return returnedPurchaseProductDao.deleteByKey(key,corpid);
    }

    /**
     * 删除采购退货单下的所有产品根据采购退货单Id
     *
     * @param returnPurchaseId id集合
     * @param corpid           公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByReturnPurchaseId(Long returnPurchaseId, String corpid) {
        return returnedPurchaseProductDao.deleteByReturnPurchaseId(returnPurchaseId,corpid);
    }

    /**
     * 删除多条采购退货单下下的所有产品
     *
     * @param returnPurchaseIdIn id集合
     * @param corpid             公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByReturnPurchaseIdIn(List<Long> returnPurchaseIdIn, String corpid) {
        return returnedPurchaseProductDao.deleteByReturnPurchaseIdIn(returnPurchaseIdIn,corpid);
    }

    /**
     * 获取一条采购退货单产品数据
     *
     * @param key    采购合同产品ID
     * @param corpid 公司id
     * @return ReturnedPurchaseProductEntity 一条数据实体
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public ReturnedPurchaseProductEntity getByKey(Long key, String corpid) {
        return returnedPurchaseProductDao.getByKey(key,corpid);
    }

    /**
     * 多参数查询多条数据
     *
     * @param param 参数集合
     * @return List<ReturnedPurchaseProductEntity> 数据集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<ReturnedPurchaseProductEntity> findEntitys(Map<String, Object> param) {
        return returnedPurchaseProductDao.findEntitys(param);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return Integer Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return returnedPurchaseProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 关联查询
     *
     * @param param 参数集合
     * @return List<ReturnedPurchaseProductEntity> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<ReturnedPurchaseProductEntity> findEntitysJoinProduct(Map<String, Object> param) {
        return returnedPurchaseProductDao.findEntitysJoinProduct(param);
    }

    @Override
    public List<ReturnedPurchaseProductEntity> getProductsByRetPurchaseId(String corpid, Long returnedPurchaseId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("returnPurchaseId", returnedPurchaseId);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, 0);
        param.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(param);
    }

    @Override
    public List<ReturnedPurchaseProductEntity> getProductsByRetPurchaseId(String corpid, Long returnedPurchaseId, List<Integer> delIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("returnPurchaseId", returnedPurchaseId);
        param.put(ParameterConstant.CORPID, corpid);
        param.put("delIn", delIn);
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    /**
     * 获取产品合同根据合同id
     *
     * @param corpid       公司id
     * @param returnedPurchaseIdIn 采购退货合同id
     * @return 数据
     */
    @Override
    public List<ReturnedPurchaseProductEntity> getReturnedPurchaseProductByReturnedPurchaseIdIn(String corpid, List<Long> returnedPurchaseIdIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("returnPurchaseIdIn", returnedPurchaseIdIn);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return returnedPurchaseProductDao.findProductNum(param);
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return returnedPurchaseProductDao.findProductNumCount(param);
    }

    @Override
    public Integer updateBatch(List<ReturnedPurchaseProductEntity> updateList, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(updateList)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ReturnedPurchaseProductEntity entity : updateList) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4ReturnedPurchaseProductEntityList(updateList);
        Integer result = returnedPurchaseProductDao.updateBatch(updateList,corpid);
        return  result;
    }

    @Override
    public Integer updateOutstockBatch(List<ReturnedPurchaseProductEntity> updateList, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(updateList)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ReturnedPurchaseProductEntity entity : updateList) {
            entity.setUpdateTime(now);
        }
        Integer result = returnedPurchaseProductDao.updateOutstockBatch(updateList,corpid);
        return  result;
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return returnedPurchaseProductDao.isProductSubAttrExist(attr, corpid, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn){
        return returnedPurchaseProductDao.summaryByAttr( field,corpid, refId, productIdIn);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field,String corpid,  Long refId, List<Long> productIdIn) {
        return returnedPurchaseProductDao.summaryByAttrFromOutSide( field,corpid, refId, productIdIn);
    }

    @Override
    public Double calcuateByAttrFromOutSide(String field1, String field2, String corpid, Long refId, List<Long> productIdIn) {
        return returnedPurchaseProductDao.calcuateByAttrFromOutSide( field1, field2, corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return returnedPurchaseProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<ReturnedPurchaseProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return returnedPurchaseProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }

    @Override
    public Integer deleteBatch(List<Long> deleteList, String corpid, Integer del) {
        if (CollectionUtils.isEmpty(deleteList)) {
            return 0;
        }
        return returnedPurchaseProductDao.deleteBatch(deleteList, corpid, del);
    }

    @Override
    public Integer updateMulti(List<ReturnedPurchaseProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ReturnedPurchaseProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4ReturnedPurchaseProductEntityList(list);
        return returnedPurchaseProductDao.updateMulti(list,corpid);
    }

    /**
     * 获取退货单关联产品
     *
     * @param returnPurchaseIds 退货单ids
     * @param corpid            公司id
     * @return 退货单关联产品
     * @author 周智
     * 创建时间： 2017-12-26 18:57
     */
    public List<ReturnedPurchaseProductEntity> getReturnedPurchaseProducts(List<Integer> returnPurchaseIds, String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("returnPurchaseIdIn", returnPurchaseIds);
        param.put("corpid", corpid);
        param.put("del", 0);
        List<ReturnedPurchaseProductEntity> returnedPurchaseProductList = findEntitys(param);

        List<Long> productIds = new ArrayList<>();
        productIds.add(-1L);
        for (ReturnedPurchaseProductEntity returnedPurchaseProduct : returnedPurchaseProductList) {
            productIds.add(returnedPurchaseProduct.getProductId());
        }

        param.clear();
        param.put("productIdIn", productIds);
        param.put("corpid", corpid);
        List<ProductEntityExt> productList = productModel.findEntitys(param);
        Map<Long, ProductEntityExt> productMap = new HashMap<>(16);
        for (ProductEntityExt product : productList) {
            productMap.put(product.getId(), product);
        }

        for (ReturnedPurchaseProductEntity returnedPurchaseProduct : returnedPurchaseProductList) {
            ProductEntityExt product = productMap.get(returnedPurchaseProduct.getProductId());
            if (product != null) {
                JSONObject dataList = product.getData();
                if (null != dataList) {
                    returnedPurchaseProduct.setProductName(dataList.getString(ProductEnum.NAME.getAttr()));
                    returnedPurchaseProduct.setProductSpecification(ProductEnum.SPECIFICATION.getAttr());
                    returnedPurchaseProduct.setProductUnit(ProductEnum.UNIT.getAttr());
                    returnedPurchaseProduct.setImageUrl(ProductEnum.THUMBNAIL.getAttr());
                    returnedPurchaseProduct.setCategoryId(StringUtil.toInt(ProductEnum.CATEGORY_ID.getAttr()));
                    returnedPurchaseProduct.setBarcode(ProductEnum.BARCODE.getAttr());
                }
            }
        }
        return returnedPurchaseProductList;
    }

    /**
     * 通过产品名称或编号查询对应的采购退货单id
     * @param corpid       公司id
     * @param nameOrNoLike 产品名称或编号
     * @return List<Long>
     */
    public List<Long> findReturnedPurchaseIds(String corpid, String nameOrNoLike) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("nameOrNoLike", nameOrNoLike);
        param.put("del", 0);
        return returnedPurchaseProductDao.findReturnedPurchaseIds(param);
    }

    @Override
    public List<ReturnedPurchaseProductEntity> getReturnedPurchaseProductById(Map<Long, Long>outstockFlowAndRefurned, String corpid){
        return returnedPurchaseProductDao.getReturnedPurchaseProductById(outstockFlowAndRefurned, corpid);
    }
}
